package net.fdukedom.epicurus.presentation.actions.pub;

import net.fdukedom.epicurus.domain.entity.User;
import net.fdukedom.epicurus.domain.util.UserHelper;
import net.fdukedom.epicurus.presentation.actions.AbstractAction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Activates user.
 *
 * @author Dmitry Y. Kochelaev
 */
public class ActivateAction extends AbstractAction {

    private UserHelper userHelper = UserHelper.getInstance();

    protected ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        User user = userHelper.findByActivationCode(request.getParameter("code"));
        if (user != null) {
            user.setIsActivated(true);
            userHelper.saveUser(user);
            return mapping.findForward("success");
        } else {
            return mapping.findForward("failed");
        }
    }
}
